package com.chengxiaoxiao.web.controller;

import com.chengxiaoxiao.constant.Constants;
import com.chengxiaoxiao.domain.Appinfo;
import com.chengxiaoxiao.domain.Appppt;
import com.chengxiaoxiao.service.AppinfoService;
import com.chengxiaoxiao.service.ApppptService;
import com.chengxiaoxiao.service.ConfigurationService;
import com.chengxiaoxiao.service.impl.AppinfoServiceImpl;
import com.chengxiaoxiao.service.impl.ApppptServiceImpl;
import com.chengxiaoxiao.service.impl.ConfigurationServiceImpl;
import com.chengxiaoxiao.utils.JSONUtils;
import com.chengxiaoxiao.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

//@WebServlet("/manager/ManagerServlet")
public class ManagerServlet extends HttpServlet {

    private AppinfoService appinfoService = new AppinfoServiceImpl();
    private ConfigurationService configurationService = new ConfigurationServiceImpl();
    private ApppptService apppptService = new ApppptServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if ("appinfo".equalsIgnoreCase(op)) {
            getAppinfo(request, response);
        } else if ("updateAppInfo".equalsIgnoreCase(op)) {
            updateAppInfo(request, response);
        } else if ("addApp".equalsIgnoreCase(op)) {
            addApp(request, response);
        } else if ("apphistory".equalsIgnoreCase(op)) {
            apphistory(request, response);
        } else if ("showppt".equalsIgnoreCase(op)) {
            showPpt(request, response);
        } else if ("updateppt".equalsIgnoreCase(op)) {
            updatePpt(request, response);
        }
    }

    /**
     * 更新PPT
     *
     * @param request
     * @param response
     */
    private void updatePpt(HttpServletRequest request, HttpServletResponse response) {

        String title1 = request.getParameter("title1");
        String pic1 = request.getParameter("pic1");
        int id1 = Integer.parseInt(request.getParameter("id1"));
        String title2 = request.getParameter("title2");
        String pic2 = request.getParameter("pic2");
        int id2 = Integer.parseInt(request.getParameter("id2"));
        String title3 = request.getParameter("title3");
        String pic3 = request.getParameter("pic3");
        int id3 = Integer.parseInt(request.getParameter("id3"));

        handlerApp(id1, pic1, title1);
        handlerApp(id2, pic2, title2);
        handlerApp(id3, pic3, title3);

        HashMap<String, String> map = new HashMap<>();
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "修改成功");
        JSONUtils.toJson(response,map);
    }

    private void handlerApp(int id1, String pic1, String title1) {
        Appppt a1 = apppptService.getbyId(id1);
        if (a1 == null) {
            Appppt app1 = new Appppt();
            app1.setPicture(pic1);
            app1.setTitle(title1);
            apppptService.add(app1);
        } else {
            Appppt ppt = apppptService.getbyId(id1);
            ppt.setPicture(pic1);
            ppt.setTitle(title1);
            apppptService.update(ppt);
        }
    }

    /**
     * 展示
     *
     * @param request
     * @param response
     */
    private void showPpt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /*int number = 0;
        try {
            number = Integer.parseInt(configurationService.getValue(Constants.APP_PPT_TOTAL_NUMBER));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        List<Configuration> list = new ArrayList<Configuration>();
        for (int i = 0; i < number; i++) {
            Configuration configuration = configurationService.getValueByKey("APP_PPT_CONFIGURATION_TITLE_" + i + 1);
            list.add(configuration);

            Configuration configuration1 = configurationService.getValueByKey("APP_PPT_CONFIGURATION_TITLE_" + i + 1);
            list.add(configuration1);

        }*/

        List<Appppt> list = apppptService.get();


        request.setAttribute("list", list);
        request.getRequestDispatcher("/manager/appppt.jsp").forward(request, response);

    }

    /**
     * 查看历史版本的APP
     *
     * @param request
     * @param response
     */
    private void apphistory(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        List<Appinfo> list = appinfoService.getAll();

        request.setAttribute("app", list);
        request.getRequestDispatcher("/manager/apphistory.jsp").forward(request, response);
    }

    /**
     * 发布新版本
     *
     * @param request
     * @param response
     */
    private void addApp(HttpServletRequest request, HttpServletResponse response) {
        String appName = request.getParameter("appName");
        String appimage = request.getParameter("appimage");
        String appVersion = request.getParameter("appVersion");
        String content = request.getParameter("content");
        String address = request.getParameter("address");

        HashMap<String, String> map = new HashMap<>();
        if (StringUtils.isNullOrEmpty(appName)) {
            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "error");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "应用名称不能为空");

            JSONUtils.toJson(response, map);
            return;
        }

        if (StringUtils.isNullOrEmpty(appVersion)) {
            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "error");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "应用版本不能为空");
            JSONUtils.toJson(response, map);
            return;
        }
        if (StringUtils.isNullOrEmpty(content)) {
            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "error");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "更新内容不能为空");
            JSONUtils.toJson(response, map);
            return;
        }

        Appinfo app = new Appinfo();
        app.setAddress(address);
        app.setName(appName);
        app.setSubTime(new Date());
        app.setUpdateContent(content);
        app.setIconAddress(appimage);
        app.setVersionNum(appVersion);

        appinfoService.add(app);

        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "发布新版本成功");
        JSONUtils.toJson(response, map);


    }

    /**
     * 更新最后一个版本的信息
     *
     * @param request
     * @param response
     */
    private void updateAppInfo(HttpServletRequest request, HttpServletResponse response) {

        String appName = request.getParameter("appName");
        String appimage = request.getParameter("appimage");
        String appVersion = request.getParameter("appVersion");
        String content = request.getParameter("content");

        HashMap<String, String> map = new HashMap<>();
        if (StringUtils.isNullOrEmpty(appName)) {
            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "error");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "应用名称不能为空");

            JSONUtils.toJson(response, map);
            return;
        }

        if (StringUtils.isNullOrEmpty(appVersion)) {
            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "error");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "应用版本不能为空");
            JSONUtils.toJson(response, map);
            return;
        }
        if (StringUtils.isNullOrEmpty(content)) {
            map.put(Constants.JSON_RESPONSE_KEY_STATUS, "error");
            map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "更新内容不能为空");
            JSONUtils.toJson(response, map);
            return;
        }

        Appinfo app = appinfoService.getLastVersion();

        app.setName(appName);
        app.setSubTime(new Date());
        app.setUpdateContent(content);
        app.setIconAddress(appimage);
        app.setVersionNum(appVersion);

        appinfoService.update(app);
        map.put(Constants.JSON_RESPONSE_KEY_STATUS, "ok");
        map.put(Constants.JSON_RESPONSE_KEY_ERRORMSG, "更新成功");
        JSONUtils.toJson(response, map);


    }

    /**
     * 获取应用信息
     *
     * @param request
     * @param response
     */
    private void getAppinfo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Appinfo app = appinfoService.getLastVersion();

        request.setAttribute(Constants.REQUEST_APPINFO_MODEL_APPINFO, app);
        request.getRequestDispatcher("/manager/appinfo.jsp").forward(request, response);
    }
}
